﻿using FastCloud.Base;
using FastCloud.Base.DB;
using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Base
{
    [Route("frame/base/config")]
    public class ConfigController : BaseController
    {
        [AutoWired]
        private DB_Config DB_Config;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/Base/Config/Config_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/Base/Config/Config_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/Base/Config/Config_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ConfigName = ValueConvert.ConvertToString(GetFormParam("ConfigName"));
            string GroupGuid = ValueConvert.ConvertToString(GetFormParam("GroupGuid"));
            return Result.OK(DB_Config.SelectPage(ConfigName, GroupGuid));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Config Entity)
        {
            if (DB_Config.ExistConfig(Entity.ConfigName))
            {
                return Result.Error("已存在相同名称的参数！");
            }
            else
            {
                DB_Config.Insert(Entity);
                return Result.OK();
            }
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Config Entity)
        {
            if (DB_Config.ExistConfig(Entity.ConfigGuid,Entity.ConfigName))
            {
                return Result.Error("已存在相同名称的参数！");
            }
            else
            {
                DB_Config.UpdateByID(Entity);
                return Result.OK();
            }
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ConfigGuid = ValueConvert.ConvertToString(GetFormParam("ConfigGuid"));
            return Result.OK(DB_Config.SelectOne(ConfigGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            DB_Config.DeleteBatch(KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
            return Result.OK();
        }

    }
}
